package test.dhadi.model;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.swing.JFrame;
import javax.swing.JPanel;

import dhadi.model.Board;
import dhadi.model.Player;

public class BoardTestMan {
	
	private Board board = null;
	private Map<String,Point> nodePoints = new HashMap<String,Point>();
	JFrame frame = null;
	JPanel canvas = null;
	
	public BoardTestMan(Board board)
	{
		this.board = board;
		renderBoard();
	}
	
    public static void main(String[] args) {
		Board b = Board.createBoard(new Player("A"), new Player("B"));
		BoardTestMan t = new BoardTestMan(b);
	}

	private void renderBoard() {
		frame = new JFrame("BoardTest");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		frame.setSize(new Dimension(500,500));
		canvas = new JPanel() {
			public void paint(Graphics g) {
				super.paint(g);
				drawBoard(g);
			}
		};
		canvas.setBackground(Color.BLACK);
		frame.add(canvas);
		frame.setVisible(true);
	}
	
	private void drawBoard(Graphics g)
	{
		g.setColor(Color.GRAY);
		
		int innerSpace = g.getClip().getBounds().width/10;
		int x = (g.getClip().getBounds().width/2)-innerSpace*3;
		int y = (g.getClip().getBounds().height/2)-innerSpace*3;		
		int w = (innerSpace)*6;
		int h = (innerSpace)*6;
		
		int midX = 0;
		int midY = 0;
		
		g.drawRect(x, y, w, h);
		this.addNodePoint("A",x,y);
		this.addNodePoint("C",x+w, y);
		this.addNodePoint("V",x, y+h);
		this.addNodePoint("X",x+w, y+h);
		
		midX = x + w/2;
		midY = y + h/2;	
		g.drawLine(midX, y, midX, y+innerSpace);
		g.drawLine(x, midY, x+innerSpace, midY);
		g.drawLine(x+w, midY, x+w-innerSpace, midY);
		g.drawLine(midX, y+h, midX, y+h-innerSpace);
		
		this.addNodePoint("B",midX, y);
		this.addNodePoint("J",x, midY);
		this.addNodePoint("O",x+w, midY);
		this.addNodePoint("W",midX, y+h);
		
		
		x += innerSpace;
		y += innerSpace;
		w -= innerSpace*2;
		h -= innerSpace*2;
		g.drawRect(x, y, w, h);
		
		this.addNodePoint("D",x,y);
		this.addNodePoint("F",x+w, y);
		this.addNodePoint("S",x, y+h);
		this.addNodePoint("U",x+w, y+h);
		
		midX = x + w/2;
		midY = y + h/2;	
		g.drawLine(midX, y, midX, y+innerSpace);
		g.drawLine(x, midY, x+innerSpace, midY);
		g.drawLine(x+w, midY, x+w-innerSpace, midY);
		g.drawLine(midX, y+h, midX, y+h-innerSpace);
		
		this.addNodePoint("E",midX, y);
		this.addNodePoint("K",x, midY);
		this.addNodePoint("N",x+w, midY);
		this.addNodePoint("T",midX, y+h);
		
		
		x += innerSpace;
		y += innerSpace;
		w -= innerSpace*2;
		h -= innerSpace*2;
		g.drawRect(x, y, w, h);
		
		this.addNodePoint("G",x,y);
		this.addNodePoint("I",x+w, y);
		this.addNodePoint("P",x, y+h);
		this.addNodePoint("R",x+w, y+h);
		midX = x + w/2;
		midY = y + h/2;
		
		this.addNodePoint("H",midX, y);
		this.addNodePoint("L",x, midY);
		this.addNodePoint("M",x+w, midY);
		this.addNodePoint("Q",midX, y+h);
		
		
		this.drawNodes(g);
		
	}

	private void drawNodes(Graphics g) {
		
		g.setColor(Color.BLACK);
		Iterator<String> i = this.nodePoints.keySet().iterator();
		Point p = null;
		while (i.hasNext())
		{
			String nodeName = i.next();
			p = this.nodePoints.get(nodeName);
			byte[] bytes = nodeName.getBytes(); 
			this.drawPic(g, p);
			g.drawBytes(bytes, 0, bytes.length, p.x-3, p.y+5);
		}
	}
	
	private void drawPic(Graphics g, Point p)
	{
		final int picWidth = 20;
		
		Color oldColor = g.getColor();
		g.setColor(Color.GRAY);
		
		int x = p.x - picWidth/2;
		int y = p.y - picWidth/2;
		g.drawOval(x, y, picWidth, picWidth);
		g.fillOval(x, y, picWidth, picWidth);
		
		g.setColor(oldColor);
	}

	private void addNodePoint(String nodeName, int x, int y) {
		this.nodePoints.put(nodeName, new Point(x,y));
	}
}
